package com.vm.osm.business.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vm.osm.business.dao.OsmOrderFailureMapper;
import com.vm.osm.business.dao.OsmOrderFlowMapper;
import com.vm.osm.business.dto.req.orderInst.ReqAcceptByDept;
import com.vm.osm.business.dto.req.orderInst.ReqGetMyOrder;
import com.vm.osm.business.dto.req.orderInst.ReqOutSideOrder;
import com.vm.osm.business.dto.req.orderInst.ReqRefuseByDept;
import com.vm.osm.business.dto.rsp.failOrder.FailOrderPage;
import com.vm.osm.business.dto.rsp.orderInst.OrderInfo;
import com.vm.osm.business.dto.rsp.orderInst.RspAllOrderPage;
import com.vm.osm.business.dto.rsp.orderInst.RspOrderPage;
import com.vm.osm.business.entity.OsmOrderFailure;
import com.vm.osm.business.entity.OsmOrderFlow;
import com.vm.osm.business.service.IOrderService;
import com.vm.osm.business.service.IOsmDispatchRuleService;
import com.vm.osm.business.util.UserDataUtil;
import com.vm.osm.business.util.UuidUtil;
import com.vma.eams.spring.boot.log.LogCollector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by chenzui on 2018/4/1.
 */
@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OsmOrderFailureMapper osmOrderFailureMapper;

    @Autowired
    private OsmOrderFlowMapper osmOrderFlowMapper;

    @Autowired
    private IOsmDispatchRuleService osmDispatchRuleService;
    @Override
    public RspAllOrderPage findPage(ReqGetMyOrder reqGetMyOrder) {
        RspAllOrderPage rspOrderPage = new RspAllOrderPage();
        Page page = PageHelper.startPage(reqGetMyOrder.getPage_num(),reqGetMyOrder.getPage_size(),true);
        List<FailOrderPage> list = osmOrderFailureMapper.queryOrderFailureByDepartment(reqGetMyOrder);
        rspOrderPage.setTotal_num(page.getTotal());
        rspOrderPage.setPage_size(page.getPageSize());
        rspOrderPage.setPage_num(page.getPageNum());
        rspOrderPage.setData_list(list);
        return rspOrderPage;
    }

    @Override
    public void upOrder(ReqOutSideOrder reqUpOrder ) {
        String orderId = reqUpOrder.getOrder_id();
        OsmOrderFailure orderFailure = osmOrderFailureMapper.selectByPrimaryKey(orderId);
        orderFailure.setStatus("11");//故障上报
        orderFailure.setTargetEnterpriseId(reqUpOrder.getEnterprise_id());
        orderFailure.setTargetEnterpriseName(reqUpOrder.getEnterprise_name());
        osmOrderFailureMapper.updateByPrimaryKeySelective(orderFailure);

        //生成派单企业的订单
        orderFailure.setEnterpriseId(reqUpOrder.getEnterprise_id());
        orderFailure.setStatus("12");//待受理
        orderFailure.setTargetEnterpriseName("");
        orderFailure.setTargetEnterpriseId("");
        orderFailure.setCreateTime(new Date());
        orderFailure.setModifyTime(new Date());
        orderFailure.setId(UuidUtil.getRandomUuid());
        osmOrderFailureMapper.insertSelective(orderFailure);
    }

    @Override
    public void acceptByDept(ReqAcceptByDept reqAcceptByDept, HttpServletRequest request) {
        String orderId = reqAcceptByDept.getOrder_id();

        OsmOrderFailure orderFailure = osmOrderFailureMapper.selectByPrimaryKey(orderId);

        if(!orderFailure.getStatus().equals("12")){
            return;
        }

        String outOrderId = orderFailure.getOutOrderId();//原企业故障单ID

        OsmOrderFailure outOrder = osmOrderFailureMapper.selectByPrimaryKey(outOrderId);

        outOrder.setModifyTime(new Date());

        outOrder.setStatus("17");//处理中

        outOrder.setAcceptStaffName(UserDataUtil.getUserSelectedEnterpriseName(request));

        osmOrderFailureMapper.updateByPrimaryKeySelective(outOrder);

        String simpleDesc = "受理工单";

        if("1".equals(reqAcceptByDept.getType())){

            //自动分发
            osmDispatchRuleService.orderAutoDispatch(orderId);

        }else if("2".equals(reqAcceptByDept.getType())){
            //直接指定
            orderFailure.setAcceptStaffId(reqAcceptByDept.getStaff_id());

            orderFailure.setStatus("5");//待接单

            outOrder.setModifyTime(new Date());

            simpleDesc+=",指派给:"+reqAcceptByDept.getStaff_name();

            osmOrderFailureMapper.updateByPrimaryKeySelective(orderFailure);

        }

        OsmOrderFlow flow = new OsmOrderFlow();
        flow.setOperateUserName(UserDataUtil.getStaffName(request));
        flow.setOperateUserId(UserDataUtil.getStaffId(request));
        flow.setOperateUserIcon(UserDataUtil.getStaffHeadImg(request));
        flow.setStatus(new Byte("1"));
        flow.setOrderId(orderFailure.getId());
        flow.setCreateTime(Integer.valueOf(String.valueOf(new Date().getTime()/1000)));
        flow.setUpdateTime(Integer.valueOf(String.valueOf(new Date().getTime()/1000)));
        flow.setId(UuidUtil.getRandomUuid());
        flow.setSimpleDesc(simpleDesc);
        osmOrderFlowMapper.insertSelective(flow);

        // TODO: 2018/4/8 工单操作日志

        LogCollector.INSTANCE.put("工单号",orderFailure.getOrderNbr());
    }

    @Override
    public void refuseByDept(String orderId,String type,String reason,HttpServletRequest request) {

        OsmOrderFailure orderFailure = osmOrderFailureMapper.selectByPrimaryKey(orderId);
        orderFailure.setStatus("9");//已关闭
        orderFailure.setModifyTime(new Date());
        osmOrderFailureMapper.updateByPrimaryKeySelective(orderFailure);

        //记录操作流程节点
        OsmOrderFlow osmOrderFlow = new OsmOrderFlow();
        osmOrderFlow.setSimpleDesc("拒绝受理:"+reason);
        osmOrderFlow.setOperateUserId(UserDataUtil.getStaffId(request));
        osmOrderFlow.setId(UuidUtil.getRandomUuid());
        osmOrderFlow.setStatus(new Byte("1"));
        osmOrderFlow.setCreateTime(Integer.valueOf(String.valueOf(System.currentTimeMillis()/1000)));
        osmOrderFlow.setUpdateTime(Integer.valueOf(String.valueOf(System.currentTimeMillis()/1000)));
        osmOrderFlow.setOperateUserName(UserDataUtil.getStaffName(request));
        osmOrderFlow.setOrderId(orderId);
        osmOrderFlow.setOperateUserIcon(UserDataUtil.getStaffHeadImg(request));
        osmOrderFlow.setRefuseType(Integer.valueOf(type));
        osmOrderFlow.setRefuseReason(reason);
        osmOrderFlowMapper.insertSelective(osmOrderFlow);

        if(orderFailure.getIsFromOut() != null && orderFailure.getIsFromOut().equals(1)){
            String sourceId = orderFailure.getOutOrderId();
            OsmOrderFailure source = osmOrderFailureMapper.selectByPrimaryKey(sourceId);
            source.setStatus("11");
            osmOrderFailureMapper.updateByPrimaryKeySelective(source);
            //记录操作流程节点
            OsmOrderFlow osmOrderFlow2 = new OsmOrderFlow();
            osmOrderFlow2.setSimpleDesc("服务商拒绝受理:"+reason);
            osmOrderFlow2.setOperateUserId(UserDataUtil.getStaffId(request));
            osmOrderFlow2.setId(UuidUtil.getRandomUuid());
            osmOrderFlow2.setStatus(new Byte("1"));
            osmOrderFlow2.setCreateTime(Integer.valueOf(String.valueOf(System.currentTimeMillis()/1000)));
            osmOrderFlow2.setUpdateTime(Integer.valueOf(String.valueOf(System.currentTimeMillis()/1000)));
            osmOrderFlow2.setOperateUserName(UserDataUtil.getStaffName(request));
            osmOrderFlow2.setOrderId(sourceId);
            osmOrderFlow2.setOperateUserIcon(UserDataUtil.getStaffHeadImg(request));
            osmOrderFlow2.setRefuseType(Integer.valueOf(type));
            osmOrderFlow2.setRefuseReason(reason);
            osmOrderFlowMapper.insertSelective(osmOrderFlow2);
        }

        LogCollector.INSTANCE.put("工单号",orderFailure.getOrderNbr());

    }
}
